<?php 

/* 	These code was generated using phpCIGen v 0.1.b (21/04/2009)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com, 
	
	+ Module  		: m_job Controller
	+ Description	: For record controller process back-end
	+ Filename 		: C_report_barang.php
 	+ Author  		: 
 	+ Created on 24/Jul/2012 11:02:41
	
*/

//class of m_job
class C_report_barang extends CI_Controller {

	//constructor
	function C_report_barang(){
		parent::__construct();
		$this->load->model("m_public_function");
		checkLogin();
	}
	
	//set index
	function index(){
        $result = $this->m_public_function->grafikBarang();
        $data = array();
        $label = array();
        foreach($result as $row){
            $data[$row->jobdet_id][] = array(
                    strtotime($row->kontrak_date) * 1000, $row->kompdet_price * 1
                );
            $label[$row->jobdet_id] = $row;
        }
        $data["data"] = $data;
        $data["label"] = $label;
		$this->load->view('main/v_report_barang',$data);
	}
	
    function detail(){
        $barang = $_POST["barang_id"];
        $result = $this->m_public_function->grafikBarangDetail($barang);
        echo json_encode($result);
    }
    
    function json(){
        $data = array(
            array(time() * 1000,100),
            array(time() * 1000 - 20000,200),
            array(time() * 1000 + 800000,300)
        );
        echo json_encode($data);
    }
	//for detail action
	//list detail handler action
	function  detail_m_jobdet_list(){
		$query = isset($_POST['query']) ? @$_POST['query'] : "";
		$start = (integer) (isset($_POST['start']) ? @$_POST['start'] : @$_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? @$_POST['limit'] : @$_GET['limit']);
		$master_id = (integer) (isset($_POST['master_id']) ? @$_POST['master_id'] : @$_GET['master_id']);
		$result=$this->m_m_job->detail_m_jobdet_list($master_id,$query,$start,$end);
		echo $result;
	}
	//end of handler
	
	//event handler action
	function get_action(){
		$task = $_POST['task'];
		switch($task){
			case "LIST":
				$this->m_job_list();
				break;
			case "UPDATE":
				$this->m_job_update();
				break;
			case "CREATE":
				$this->m_job_create();
				break;
			case "DELETE":
				$this->m_job_delete();
				break;
			case "SEARCH":
				$this->m_job_search();
				break;
			case "PRINT":
				$this->m_job_print();
				break;
			case "EXCEL":
				$this->m_job_export_excel();
				break;
			default:
				echo "{failure:true}";
				break;
		}
	}
	
	//function fot list record
	function m_job_list(){
		
		$query = isset($_POST['query']) ? @$_POST['query'] : "";
		$start = (integer) (isset($_POST['start']) ? @$_POST['start'] : @$_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? @$_POST['limit'] : @$_GET['limit']);
		$result=$this->m_m_job->m_job_list($query,$start,$end);
		echo $result;
	}
	
	function m_job_print(){
  		//POST varibale here
		$job_id=trim(@$_POST["job_id"]);
		$job_name=trim(@$_POST["job_name"]);
		$job_name=str_replace("/(<\/?)(p)([^>]*>)", "",$job_name);
		$job_name=str_replace("'", "'",$job_name);
		$job_active=trim(@$_POST["job_active"]);
		$job_active=str_replace("/(<\/?)(p)([^>]*>)", "",$job_active);
		$job_active=str_replace("'", "'",$job_active);
		$job_created_date=trim(@$_POST["job_created_date"]);
		$job_created_by=trim(@$_POST["job_created_by"]);
		$job_created_by=str_replace("/(<\/?)(p)([^>]*>)", "",$job_created_by);
		$job_created_by=str_replace("'", "'",$job_created_by);
		$job_updated_date=trim(@$_POST["job_updated_date"]);
		$job_updated_by=trim(@$_POST["job_updated_by"]);
		$job_updated_by=str_replace("/(<\/?)(p)([^>]*>)", "",$job_updated_by);
		$job_updated_by=str_replace("'", "'",$job_updated_by);
		$job_updated_count=trim(@$_POST["job_updated_count"]);
		$job_updated_count=str_replace("/(<\/?)(p)([^>]*>)", "",$job_updated_count);
		$job_updated_count=str_replace("'", "'",$job_updated_count);
		$m_div_div_id=trim(@$_POST["m_div_div_id"]);
		$option=$_POST['currentlisting'];
		$filter=$_POST["query"];
		
		$data["data_print"] = $this->m_m_job->m_job_print($job_id ,$job_name ,$job_active ,$job_created_date ,$job_created_by ,$job_updated_date ,$job_updated_by ,$job_updated_count ,$m_div_div_id ,$option,$filter);
		$print_view=$this->load->view("main/p_m_job.php",$data,TRUE);
		if(!file_exists("print")){
			mkdir("print");
		}
		$print_file=fopen("print/m_job_printlist.html","w+");
		fwrite($print_file, $print_view);
		echo '1';        
	}
	/* End Of Function */

	/* Function to Export Excel document */
	function m_job_export_excel(){
		//POST varibale here
		$job_id=trim(@$_POST["job_id"]);
		$job_name=trim(@$_POST["job_name"]);
		$job_name=str_replace("/(<\/?)(p)([^>]*>)", "",$job_name);
		$job_name=str_replace("'", "\'",$job_name);
		$job_active=trim(@$_POST["job_active"]);
		$job_active=str_replace("/(<\/?)(p)([^>]*>)", "",$job_active);
		$job_active=str_replace("'", "\'",$job_active);
		$job_created_date=trim(@$_POST["job_created_date"]);
		$job_created_by=trim(@$_POST["job_created_by"]);
		$job_created_by=str_replace("/(<\/?)(p)([^>]*>)", "",$job_created_by);
		$job_created_by=str_replace("'", "\'",$job_created_by);
		$job_updated_date=trim(@$_POST["job_updated_date"]);
		$job_updated_by=trim(@$_POST["job_updated_by"]);
		$job_updated_by=str_replace("/(<\/?)(p)([^>]*>)", "",$job_updated_by);
		$job_updated_by=str_replace("'", "\'",$job_updated_by);
		$job_updated_count=trim(@$_POST["job_updated_count"]);
		$job_updated_count=str_replace("/(<\/?)(p)([^>]*>)", "",$job_updated_count);
		$job_updated_count=str_replace("'", "\'",$job_updated_count);
		$m_div_div_id=trim(@$_POST["m_div_div_id"]);
		$option=$_POST['currentlisting'];
		$filter=$_POST["query"];
		$data["type"]="excel";
		$data["data_print"] = $this->m_m_job->m_job_export_excel($job_id ,$job_name ,$job_active ,$job_created_date ,$job_created_by ,$job_updated_date ,$job_updated_by ,$job_updated_count ,$m_div_div_id ,$option,$filter);
		$print_view=$this->load->view("main/p_m_job.php",$data,TRUE);
		if(!file_exists("print")){
			mkdir("print");
		}
		$print_file=fopen("print/m_job_printlist.xls","w+");
		fwrite($print_file, $print_view);
		echo '1';  
			
	}
	
	// Encodes a SQL array into a JSON formated string
	function JEncode($arr){
		if (version_compare(PHP_VERSION,"5.2","<"))
		{    
			require_once("./JSON.php"); //if php<5.2 need JSON class
			$json = new Services_JSON();//instantiate new json object
			$data=$json->encode($arr);  //encode the data in json format
		} else {
			$data = json_encode($arr);  //encode the data in json format
		}
		return $data;
	}
	
	// Decode a SQL array into a JSON formated string
	function JDecode($arr){
		if (version_compare(PHP_VERSION,"5.2","<"))
		{    
			require_once("./JSON.php"); //if php<5.2 need JSON class
			$json = new Services_JSON();//instantiate new json object
			$data=$json->decode($arr);  //decode the data in json format
		} else {
			$data = json_decode($arr);  //decode the data in json format
		}
		return $data;
	}
	
	
}
?>